import http from '@/api/http.js'
const zhifubaoPay = function(orderData,successFun='',failFun = '') {
	let ordersn = orderData.ordersn,
		orderid = orderData.orderid,
		pageType = orderData.pageType ? orderData.pageType : 0; // 页面跳转模式 默认0为直接跳转 1为关闭当前页面跳转
	let params = {
		method: 'api/v2/payment/zhifubao/get_pay_data',
		ordersn:ordersn
	};
	uni.showLoading({
		title:'正在发起支付'
	});
	http(params).then(payRes => {
		uni.hideLoading();
		let payResult = payRes.data;

		if(payRes.status){
			my.tradePay({
				tradeNO: payResult.trade_no,  
				success: function(res){
					console.log(res,res.resultCode,failFun,321);
					let extendInfo = res.extendInfo;
					if(res.resultCode==9000){
						if(successFun)
						{
							setTimeout(()=>{
								successFun();
							},2000);
						}
					}else{
						uni.showToast({
							title: "支付失败",
							icon: 'none',
							duration: 2000
						});
						// 失败执行事件
						if(failFun)
						{
							setTimeout(()=>{
								failFun();
							},2000);
						}
					}
				},
				fail: function(res){
					uni.showToast({
						title: "支付已取消",
						icon: 'none',
						duration: 2000
					});
					// 失败执行事件
					if(failFun)
					{
						setTimeout(()=>{
							failFun();
						},2000);
					}
				},
			});
		}else{
			let data = payRes.data;
			// 如果是已支付 或者 0元支付成功回调
			if (data.sign) {
				if (data.sign == 24 && data.id) { // 支付成功
					uni.redirectTo({
						url: `/pages/order/paysuccess?ordersn=${data.ordersn}&orderid=${data.id}`
					})
					return;
				}else{
					tt.showToast({
						title: payRes.msg,
						icon: 'fail'
					});
				}
			}
			else
			{
				tt.showToast({
					title: payRes.msg,
					icon: 'fail'
				});
			}
		}
	});
}


export default zhifubaoPay
